package cl.kts.dc.actor;

import cl.kts.dc.util.Constantes;
import cl.kts.dc.util.FrameUtil;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;

/**
 *
 * @author Francisco
 */
public class MaloActor extends Actor {

    private final Rectangle contorno;

    private TextureRegion currentFrame;

    private Animation correDerAnimacion;
    private Animation paradoDerAnimacion;
    private Animation saltoDerAnimacion;
    private Animation correIzqAnimacion;
    private Animation paradoIzqAnimacion;
    private Animation caidaDerAnimacion;
    private Animation saltoIzqAnimacion;
    public boolean correrConEsenario = false;

    private float destX;
    private float stateTime;

    private MoveToAction accion;
    private Animation caidaIzqAnimacion;

    public enum estado {

        esperando, corriendo, caminando, disparando, saltando, cayendo
    }

    public enum direccion {

        izquierda, derecha, arriba, abajo
    }

    public estado st = estado.esperando;
    public direccion dir = direccion.derecha;

    public MaloActor() {
        setBounds(600, Constantes.ALTURA_PISO, 100, 140);
        contorno = new Rectangle(getX(), getY(), 100, 140);
        cargarAnimaciones();
    }

    private void cargarAnimaciones() {
        stateTime += Gdx.graphics.getDeltaTime();
        correDerAnimacion = new Animation(0.02f, FrameUtil.cortarTextura("monocorre.png", 12, 2, false));
        correIzqAnimacion = new Animation(0.02f, FrameUtil.cortarTextura("monocorre.png", 12, 2, true));

        paradoDerAnimacion = new Animation(0.05f, FrameUtil.cortarTextura("flaite.png", 10, 1, false));
        paradoIzqAnimacion = new Animation(0.05f, FrameUtil.cortarTextura("flaite.png", 10, 1, true));

        paradoDerAnimacion.setPlayMode(Animation.PlayMode.REVERSED);
        

        saltoDerAnimacion = new Animation(0.05f, FrameUtil.cortarTextura("salto.png", 11, 1, false));
        saltoIzqAnimacion = new Animation(0.05f, FrameUtil.cortarTextura("salto.png", 11, 1, true));

        caidaDerAnimacion = new Animation(0.05f, FrameUtil.cortarTextura("caida.png", 10, 1, false));
        caidaIzqAnimacion = new Animation(0.05f, FrameUtil.cortarTextura("caida.png", 10, 1, true));

    }

    public void corre() {

        if (st == estado.cayendo || st == estado.saltando) {
            return;
        }
        st = estado.corriendo;

        if (dir == direccion.derecha) {
            destX = this.getX() + 100;
        } else {
            destX = this.getX() - 100;
        }
        accion = new MoveToAction();
        accion.setPosition(destX, this.getY());
        accion.setDuration(0.3f);

        addAction(accion);
    }
//todo

    public void salta() {

        if (st == estado.cayendo || st == estado.saltando) {
            return;
        }

        SequenceAction seq;
        st = estado.saltando;

        if (dir == direccion.izquierda) {
            destX = this.getX() + (200 * -1);
            seq = getSaltoAnimation(-1, 200);
        } else {
            destX = this.getX() + (200 * 1);
            seq = getSaltoAnimation(1, 200);
        }

        stateTime = 0;
        addAction(seq);
    }

    public void caer() {

        if (st == estado.cayendo) {
            return;
        }
        
        st = estado.cayendo;

        SequenceAction seq = new SequenceAction();
        float destXtemp = 1;
        if (dir == direccion.izquierda) {
            destXtemp = -1;
        }

        destX = this.getX() + (50 * destXtemp);

        MoveToAction caoida2 = new MoveToAction();
        caoida2.setPosition(this.getX() + (50 * destXtemp), Constantes.ALTURA_PISO);
        caoida2.setDuration(0.5f);

        seq.addAction(caoida2);
        stateTime = 0;
        addAction(seq);
    }

    @Override
    public void act(float delta) {
        super.act(delta);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {

        stateTime += Gdx.graphics.getDeltaTime();

        if (getX() == destX) {
            st = estado.esperando;
        }

        switch (st) {
            case cayendo:
                if (dir == direccion.derecha) {
                    currentFrame = caidaDerAnimacion.getKeyFrame(stateTime, true);
                } else {
                    currentFrame = caidaIzqAnimacion.getKeyFrame(stateTime, true);
                }

                break;
            case corriendo:
                if (dir == direccion.derecha) {
                    currentFrame = correDerAnimacion.getKeyFrame(stateTime, true);
                } else {
                    currentFrame = correIzqAnimacion.getKeyFrame(stateTime, true);
                }
                break;

            case saltando:

                if (dir == direccion.derecha) {
                    currentFrame = saltoDerAnimacion.getKeyFrame(stateTime, true);
                } else {
                    currentFrame = saltoIzqAnimacion.getKeyFrame(stateTime, true);
                }
                break;

            default:
                if (dir == direccion.derecha) {
                    currentFrame = paradoDerAnimacion.getKeyFrame(stateTime, true);
                } else {
                    currentFrame = paradoIzqAnimacion.getKeyFrame(stateTime, true);
                }
        }

        contorno.set(this.getX(), this.getY(), 80, 100);
        batch.draw(currentFrame, this.getX(), this.getY(), this.getWidth(), this.getHeight());
    }

    public boolean seAlcanzoLimite() {

        if (getX() + 500 > Constantes.ANCHO_MAXIMO) {
            return true;
        }
        return false;
    }

    public Rectangle getContorno() {
        return contorno;
    }

    public SequenceAction getSaltoAnimation(float direccion, float alcance) {

        SequenceAction seq = new SequenceAction();
        MoveToAction subidaAction;
        MoveToAction bajadaAction;
        MoveToAction mantAction;

        subidaAction = new MoveToAction();
        mantAction = new MoveToAction();
        bajadaAction = new MoveToAction();

        float destXsubida = alcance / 2;
        float destXman = (alcance / 2) + 50;
        float destXbajada = alcance;

        subidaAction.setPosition(this.getX() + (destXsubida * direccion), this.getY() + 100);
        subidaAction.setDuration(0.2f);

        mantAction.setPosition(this.getX() + (destXman * direccion), this.getY() + 100);
        mantAction.setDuration(0.1f);

        bajadaAction.setPosition(this.getX() + (destXbajada * direccion), Constantes.ALTURA_PISO);
        bajadaAction.setDuration(0.2f);

        seq.addAction(subidaAction);
        seq.addAction(mantAction);
        seq.addAction(bajadaAction);

        return seq;
    }
}
